package cn.wolfcode.producer_test.api._02_send_mode;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.CountDownLatch;

public class Producer1 {
    public static void main(String[] args) throws Exception {

        //创建一个生产者
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("wolfcode-producer");
        //绑定namesrv地址
        defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");
        //开启生产者
        defaultMQProducer.start();

        CountDownLatch countDownLatch = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {

            //创建一个消息
            Message message = new Message("_02_send_mode", "Synchronize-send".getBytes());
            //生产者发送消息
            defaultMQProducer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println("发送成功");
                    countDownLatch.countDown();
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println("发送失败");
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
        //关闭资源
        defaultMQProducer.shutdown();
    }
}
